package com.hyopt.modules.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyopt.modules.app.dao.MapDefenceDao;
import com.hyopt.modules.app.entity.ChannelInfoEntity;
import com.hyopt.modules.app.entity.MapDefenceEntity;
import com.hyopt.modules.app.entity.UserEntity;
import com.hyopt.modules.app.service.ChannelInfoService;
import com.hyopt.modules.app.service.DefenceService;
import com.hyopt.modules.app.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName DefenceServiceImpl
 * @Description TODO
 * @Author yangjun
 * @date 2020/1/3 11:16
 * @Version 1.0
 **/
@Service
public class DefenceServiceImpl extends ServiceImpl<MapDefenceDao, MapDefenceEntity> implements DefenceService {

    @Autowired
    private UserService userService;


    @Override
    public List getAllDefenceInfo(Integer uId) {
        List result=new ArrayList();
        List<Map> list=null;
        if(uId!=null){
            UserEntity user=userService.getBaseMapper().selectOne(new QueryWrapper<UserEntity>().eq("ID",uId));
            if(user.getGroupnumber()==1){
                list=this.baseMapper.getAllDefenceInfo(null);
            }else{
                list=this.baseMapper.getAllDefenceInfo(uId);
            }
        }else{
            list=this.baseMapper.getAllDefenceInfo(null);
        }
        Map<String,Map> resultMap=new HashMap<>();
        if(list!=null&&list.size()>0 && uId !=1){
            for(int i=0;i<list.size();i++){
                Map m=list.get(i);
                String defenceNo=m.get("DefenceNO").toString();
                String longitude=m.get("Longitude").toString();
                String latitude=m.get("Latitude").toString();
                m.remove("Longitude");
                m.remove("Latitude");

                Map gps=new HashMap();
                gps.put("ChannelNO",m.get("ChannelNO").toString());
                gps.put("Longitude",latitude);
                gps.put("Latitude",longitude);
                if(ObjectUtil.isNull(resultMap.get(defenceNo))){
                    List gpsList=new ArrayList();
                    gpsList.add(gps);
                    m.put("gps",gpsList);
                    resultMap.put(defenceNo,m);
                }else{
                    List pos=(List) resultMap.get(defenceNo).get("gps");
                    pos.add(gps);
                }
            }
        }else{
            return  list;
        }
        return resultMap.values().stream().collect(Collectors.toList());
    }

    @Override
    public List findDefenceInfoById(Integer uId) {
        List result=null;
        UserEntity user=userService.getBaseMapper().selectOne(new QueryWrapper<UserEntity>().eq("ID",uId));
        if(user!=null){
            if(user.getGroupnumber()==1){
                result=this.baseMapper.findDefenceInfoById(null);
            }else{
                result=this.baseMapper.findDefenceInfoById(uId);
            }
        }
        return result;
    }
}
